package cn.powerone.scm.controller;

import java.io.IOException;
import java.util.List;

import cn.powerone.scm.domain.ScmCustomer;
import cn.powerone.scm.domain.ScmProjects;
import cn.powerone.scm.service.IScmCustomerService;
import cn.powerone.scm.service.IScmProjectsService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmCustomerQuotes;
import cn.powerone.scm.service.IScmCustomerQuotesService;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;

/**
 * 客户报价单Controller
 * 
 * @author PowerOne
 * @date 2025-07-02
 */
@RestController
@RequestMapping("/scm/scmCustomerQuotes")
public class ScmCustomerQuotesController extends BaseController
{
    @Autowired
    private IScmCustomerQuotesService scmCustomerQuotesService;

    @Autowired
    private IScmProjectsService scmProjectsService;

    @Autowired
    private IScmCustomerService scmCustomerService;

    /**
     * 查询客户报价单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmCustomerQuotes scmCustomerQuotes)
    {
        startPage();
        List<ScmCustomerQuotes> list = scmCustomerQuotesService.selectScmCustomerQuotesList(scmCustomerQuotes);
        return getDataTable(list);
    }

    /**
     * 导出客户报价单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:export')")
    @Log(title = "客户报价单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmCustomerQuotes scmCustomerQuotes)
    {
        List<ScmCustomerQuotes> list = scmCustomerQuotesService.selectScmCustomerQuotesList(scmCustomerQuotes);
        ExcelUtil<ScmCustomerQuotes> util = new ExcelUtil<ScmCustomerQuotes>(ScmCustomerQuotes.class);
        util.exportExcel(response, list, "客户报价单数据");
    }

    /**
     * 生成客户报价单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:generate')")
    @Log(title = "客户报价单", businessType = BusinessType.GENORDER)
    @PostMapping("/generate")
    public void generate(HttpServletResponse response, ScmCustomerQuotes scmCustomerQuotes) throws IOException {
        ScmProjects scmProjects = scmProjectsService.selectScmProjectsByProjectId(scmCustomerQuotes.getProjectId());
        ScmCustomer scmCustomer = scmCustomerService.selectScmCustomerById(scmProjects.getCustomerId());
        List<ScmCustomerQuotes> list = scmCustomerQuotesService.selectScmCustomerQuotesList(scmCustomerQuotes);
        scmCustomerQuotesService.exportExcelByTemplate(response, scmProjects, scmCustomer, list, "报价_test.xlsx");
    }

    /**
     * 获取客户报价单详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmCustomerQuotesService.selectScmCustomerQuotesById(id));
    }

    /**
     * 新增客户报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:add')")
    @Log(title = "客户报价单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmCustomerQuotes scmCustomerQuotes)
    {
        return toAjax(scmCustomerQuotesService.insertScmCustomerQuotes(scmCustomerQuotes));
    }

    /**
     * 修改客户报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:edit')")
    @Log(title = "客户报价单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmCustomerQuotes scmCustomerQuotes)
    {
        return toAjax(scmCustomerQuotesService.updateScmCustomerQuotes(scmCustomerQuotes));
    }

    /**
     * 删除客户报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerQuotes:remove')")
    @Log(title = "客户报价单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmCustomerQuotesService.deleteScmCustomerQuotesByIds(ids));
    }
}
